import java.util.Arrays;
import java.util.Scanner;

public class NGuessNumber {
    public static void main(String[] args) {
        System.out.println("---新猜数字游戏---");
        while(true) {
            System.out.println("玩家输入1开始游戏,0结束游戏:");
            Scanner scann = new Scanner(System.in);
            switch (scann.nextInt()) {
                case 0:
                    System.out.println("已退出游戏!\n");
                    System.exit(0);
                    break;
                case 1:
                    int cs = 0;
                    int number = 7;
                    System.out.println("开始游戏(限制次数为7次):");
                    generateAnswer();
                    while (true) {
                        getPlayerInput();
                        compareGuessAnswer();
                        if (isWin() == true) {
                            System.out.println("玩家获胜!");
                            break;
                        } else {
                            cs++;
                            number--;
                            if(cs==7){
                                System.out.println("第7次到了，正确答案为："+Arrays.toString(numbers));
                                break;
                            }
                            System.out.println("猜错了，在来一次！(还剩"+number+"次)：");
                        }
                    }
                    break;
                default:
                    System.out.println("错的输入字符，请输入0/1:\n");
            }
        }
    }
    public static int[] numbers=new int[4];
    public static int[] generateAnswer(){
        int sz = 0;
        while (true) {
            int random = (int)(Math.random() * 9 + 1);
            int a = 0;
            for (a = 0 ; a < numbers.length; a++) {
                if(random == numbers[a]) {
                    break;
                }
            }
            if(a == numbers.length ) {
                numbers[sz++] = random;
            }
            if (sz == 4) {
                break;
            }
        }
        return numbers;
    }

    public static int[] guess=new int[4];
    public static int[] getPlayerInput(){
        try {
            Scanner scan = new Scanner(System.in);

            for (int i = 0; i < 4; i++) {
                System.out.println("请输入0-9之间的第" + (i+1) + "个数字：");

                int lin = scan.nextInt();
                if (lin >= 10 || lin < 0) {
                    System.out.print("输入错误!");
                    i=i-1;
                }else {
                    guess[i] = lin;
                }
            }
        }catch (Exception e){
            System.out.println("输入错误!");
        }
        System.out.println(Arrays.toString(guess)+"\n");
        return guess;
    }

    public static int one = 0;
    public static int two = 0;
    public static String compareGuessAnswer(){
        one = 0;
        two = 0;
        for (int i = 0; i < numbers.length; i++) {
            for (int l=0; l < numbers.length; l++){
                if(i!=l){
                    if(numbers[i]==guess[l]){
                        two++;
                    }
                }else{
                    if(numbers[i]==guess[l]){
                        one++;
                    }
                }
            }
        }
        System.out.println(one+"A"+two+"B");
        System.out.println("A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！");
        return one+"A"+two+"B\n"+"A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！\n"+"如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。";
    }

    public static boolean isWin(){
        boolean cs = false;
        if(one==4){
            cs=true;
        }else {
            cs=false;
        }
        return cs;
    }

}
